const dayjs = require('dayjs');
const fs = require('fs-extra');
const now = Date.now();

class Logger {
	constructor(workspace = process.cwd(), mode = 'file') {
		this.instance = null;

		this.workspace = workspace;
		this.ws = fs.createWriteStream(workspace + '/logs/out_' + now + '.log');
		this.cws = fs.createWriteStream(workspace + '/logs/crash_' + now + '.log');
		this.mode = mode;
	}

	log(str) {
		this.mode === 'file' ? this.ws.write(`[${dayjs().format('YYYY-MM-DD HH:mm:ss')}]` + str) : null;
	}

	crash(str) {
		this.mode === 'file' ? this.cws.write(`[${dayjs().format('YYYY-MM-DD HH:mm:ss')}]` + str): null;
	}

	static getInstance() {
		if (!this.instance) {
			this.instance = new Logger();
		}

		return this.instance;
	}
}

module.exports = Logger.getInstance();